package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.NewsDesActivity;
import com.winz.cultureapp.adapter.xrcv.NewsDataAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.NewsListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 文化志愿服务--新闻资讯
 */
public class VolunteerNewsFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.xrcv_news)
    public XRecyclerView mXrcv_news;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;

    private List<NewsListBean.DataBean.ListBean> mDatas;
    private NewsDataAdapter mAdapter;

    public static VolunteerNewsFragment getInstance(){
        VolunteerNewsFragment fragment = new VolunteerNewsFragment();
        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_news.setLayoutManager(manager);
        mXrcv_news.setPullRefreshEnabled(true);
        mXrcv_news.setLoadingMoreEnabled(false);
        mAdapter = new NewsDataAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_news_list);
    }

    @Override
    protected void initEvents() {

        mXrcv_news.setAdapter(mAdapter);
        mXrcv_news.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void loadDatas() {
        getList();
    }


    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();

        HttpUitls.onGet("volunteer_news", RequestFactory.getInstance().news("0", page + "", limit + ""), new OnResponeListener<NewsListBean>() {
            @Override
            public void onSuccess(NewsListBean bean) {
                Logger.d("volunteer_news==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0){
//                            totalPage = bean.data.totalPage;
                    if (bean.data.list != null && bean.data.list.size()>0){
                        mXrcv_news.setLoadingMoreEnabled(true);
                        if (isRefresh){
                            mDatas.clear();
                            mDatas = bean.data.list;
                        }else {
                            mDatas.addAll(bean.data.list);
                        }
                    }else {
                        mDatas.clear();
                        NewsListBean.DataBean.ListBean empty = new NewsListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_news.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                }else {
                    showMsg(bean.msg);
                }
                if (isRefresh){
                    mXrcv_news.refreshComplete();
                }else {
                    mXrcv_news.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                showMsg(error);
                if (isRefresh){
                    mXrcv_news.refreshComplete();
                }else {
                    mXrcv_news.loadMoreComplete();
                }
            }
        });

    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadDatas();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_news.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            loadDatas();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).volunteerId);
        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,mDatas.get(position).title);
        bundle.putString(Constants.KEY.NEWS.HREF,Constants.URL.IP + "/volunteer/detail/" + mDatas.get(position).volunteerId);
        startActivity(NewsDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
